import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import {target} from "@/utils/config";

let users = window.localStorage.getItem(`users-${target}`)
if(users) {
	users = JSON.parse(users)
}
//  业务路由
const pageRouterArr = []
const pageFiles = require.context('./page', true, /\.js$/)
pageFiles.keys().forEach(key => {
  // 正则找到key
  if(pageFiles(key).default) {
    // pageRouterArr.push(pageFiles(key).default)
  } else {
    let keyStr = key.replace('.js', '').replace('./', '')
    console.log('pageFiles(key)[keyStr] ---', pageFiles(key)[keyStr])
    pageFiles(key)[keyStr].forEach((item) => {
      pageRouterArr.push(item)
    })
  }
})
// 根据名称放菜单顺序
const menuSort = [
  'distribute', 'scene', 'plan', 'captions', 'terminal', 'platformAuth', 'audit', 'logsManager'
]
let sortedMenuArr = []

menuSort.forEach((item, key) => {
  const tmpMenu = pageRouterArr.filter((result) => {
    return result.name === item
  })

  sortedMenuArr.push(tmpMenu[0])
  if (tmpMenu.length > 1) {
    this.$message.error('菜单配置重名, 影响排序请检查')
  }
})

console.log('菜单路由顺序', sortedMenuArr, pageRouterArr)
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * 所有权限通用路由表
 * 如首页和登录页和一些不用权限的公用页面
 */

// console.log('page ---', JSON.stringify(pageRouterArr))
export const constantRoutes = [
  // 在不刷新页面的情况下更新页面
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true
  },

  {
    path: '/terminalNewHome',
    component: () => import('@/views/terminal/newHome.vue'),
    hidden: true
  },
  {
    path: '/terminal/terminalDetail',
    component: () => import('@/views/terminalDetail/index.vue'),
    hidden: true
  },
  {
    path: '/terminal/terminalDetailVertical',
    component: () => import('@/views/terminalDetail/terminalDetailVertical.vue'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
	  name: 'dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'home' }
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    name: 'dashboard',
    hidden: true,
    children: [

    ]
  },
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

// 异步挂载的路由
// 动态需要根据权限加载的路由表
// console.log('pageRouterArr ---', pageRouterArr)
// 面包屑 redirect: '/systemManage/peopleManage',
console.log('sortedMenuArr ---', sortedMenuArr)
export const asyncRoutes = [
  ...sortedMenuArr,
  // ...pageRouterArr,
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]


let baseUrl = process.env.BASE_URL
console.log('process ---', process.env.NODE_ENV)
// 实例化vue的时候只挂载constantRouter
const createRouter = () => new Router({
  mode: 'history', // //后端支持可开
  // base: '/sass/jzkc/',
  // base: '/cjht/jzkc',
  base: '/jzkc',
  // base: process.env.NODE_ENV === "development"?'/jzkc': baseUrl.indexOf('sass') !== -1?'/sass/jzkc/': '/cjht/jzkc',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// 重置路由  Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
	router.matcher = newRouter.matcher // reset router
}

export default router
